﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;

public partial class UserControl_ucAccountInfo : System.Web.UI.UserControl
{
    TblAccountsDAO userDAO = new TblAccountsDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session[Global.SESSION_LOGGED_USER] == null)
            {
                Response.Redirect("~/en/Login.aspx");
                return;
            }
            else
            {
                LoadDetailUser();
                LoadCountry();
            }
        }
    }

    private void LoadCountry()
    {
        DataSet DSSCountry = new DataSet();
        DSSCountry.ReadXml(Server.MapPath("../App_Data/countries.xml"));
        drcountry.DataSource = DSSCountry;
        drcountry.DataTextField = "name";
        drcountry.DataValueField = "code";
        drcountry.DataBind();
    }
    private void LoadDetailUser()
    {
        try
        {
            TblAccounts user = (TblAccounts)Session[Global.SESSION_LOGGED_USER];
            txtEmail.Text = user.EmailAcc;
            txtFName.Text = user.FirstName;
            txtSName.Text = user.LastName;
            txtuser.Text = user.UsernameAcc;
            txtPhone.Text = user.PhoneNumber;

            

        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
    }


    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        bool bFlag = false;
        TblAccounts userAccount = null;
        try {
                    
                    userAccount = (TblAccounts)Session[Global.SESSION_LOGGED_USER];
       
                    userAccount.FirstName = txtFName.Text ;
                    userAccount.LastName = txtSName.Text ;
                    userAccount.Country = drcountry.SelectedItem.Text;
                    userAccount.PhoneNumber = txtPhone.Text;

                    userDAO.InsUpdAccounts(userAccount, 2); 
                    Session[Global.SESSION_LOGGED_USER] = userAccount ;

                
                    bFlag = true;
                   
             }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
        if (bFlag)
            lblMessage.Text = "Update Sucessful !";
    }   
}
